package com.stx.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.stx.dao.FilmDao;
import com.stx.entity.Film;

public class FilmDaoImpl implements FilmDao {

	private HibernateTemplate sessionFactory;

	public HibernateTemplate getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(HibernateTemplate sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * 添加电影
	 * */
	@Override
	public void addFilm(Film film) {
		sessionFactory.save(film);
		sessionFactory.flush();
	}
	/**
	* 使用hql 语句进行操作
	* @param hql HSQL 查询语句
	* @param offset 开始取数据的下标
	* @param length 读取数据记录数
	* @return List 结果集
	*/
	public List getListForPage(final String hql, final int offset,
	final int length) {
	List list =sessionFactory.executeFind(new HibernateCallback() {
	public Object doInHibernate(Session session)
	throws HibernateException, SQLException {
	Query query = session.createQuery(hql);
	query.setFirstResult(offset);
	query.setMaxResults(length);
	List list = query.list();
	return list;
	}
	});
	return list;
	}

	@Override
	public List getFilmList() {
	String hql="from Film as film where film.fi_state='1' order by film.fi_releasetime desc";
	return getListForPage(hql,0,3);
	}

	@Override
	public Film getFilm(Film film) {
		film=sessionFactory.get(Film.class,film.getFi_id());
		return film;
	}

	@Override
	public List getSomeList(int firstIndex,int countPage) {
		String hql="from Film as film order by film.fi_releasetime desc";
		return getListForPage(hql,firstIndex,countPage);
	}

	@Override
	public int getCountFilmList() {
		String hql="select count(*) from Film as film";
		return ((Long)sessionFactory.find(hql).iterator().next()).intValue();
	}

    public<T> List<T> excuteSQLQuery(String sql, Object[] obj, Class<T> entityType){
        final String tempSql = sql;
        final Object[] tempObj = obj;
        final Class<T> tempEntity = entityType;
        
        return sessionFactory.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                SQLQuery q = session.createSQLQuery(tempSql);// 原文: SQLQuery q = getSession().createSQLQuery(tempSql);
                if(tempObj != null && tempObj.length > 0){
                    for(int i = 0;i < tempObj.length; i++){
                        q.setParameter(i, tempObj[i]);
                    }
                }
                return q.addEntity(tempEntity).list();
            }
        });
        
    }
	@Override
	public List getRandomFilmList(int limit) {
		String sql="select *from(select * from fc_film where fi_state=? order by dbms_random.value)where rownum <= ?";
		Object[] obj={1,limit};
		return excuteSQLQuery(sql, obj, Film.class);
	}

	@Override
	public void updateFilm(Film film) {
		sessionFactory.update(film);
		sessionFactory.flush();
	}

	@Override
	public List getYitongFilmList(int firstIndex, int countPage) {
		String hql="from Film as film where film.fi_state='5' order by film.fi_releasetime desc";
		return getListForPage(hql,firstIndex,countPage);
	}

	@Override
	public void delFilm(Film film) {
		sessionFactory.delete(film);
		sessionFactory.flush();
	}
}
